<template>
  <div>
    <yt-crud
      v-bind="options"
      ref="crudRef"
      @onLoad="getData"
      :fun-props="{
        addBtn: false,
        exportBtn: false,
        delBtn: false,

      }"
      :table-props="{
        selection: false,
        delBtn: false,
        viewBtn: false,
        editBtn: false,
        menuSlot: true,
        border: true,
        menuWidth: 100,
        pageHide: true,
      }"
      :loading="state.loading"
      :total="state.total"
      v-model:page="state.page"
      v-model:query="state.query"
    >
      <template #rightToolbar></template>
      <template #menuSlot="scope:any">
        <span>
          <el-tooltip class="box-item" effect="dark" content="配置" placement="top">
            <el-button link type="primary" icon="Setting" @click="handleProperties(scope.row)" />
          </el-tooltip>
        </span>
      </template>
      <template #status="scope:any">
        <el-switch v-model="scope.row.status" active-value="Enabled" inactive-value="Disabled" @change="handleStatusChange(scope.row)" />
      </template>
    </yt-crud>

    <WechatDialog ref="wechatRef" @success="handleSuccess" />
    <MbankDialog ref="mbankRef" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup>
import { IColumn } from '@/components/common/types/tableCommon'
import { getPaymentList, changeStatus } from '@/api/system/payment'

import WechatDialog from './modules/WechatDialog.vue'
import MbankDialog from './modules/MbankDialog.vue'

import YtCrud from '@/components/common/yt-crud.vue'

import {ComponentInternalInstance} from 'vue'
import {PaymentVO} from '@/api/system/payment/types'
import {useI18n} from 'vue-i18n'

const { t } = useI18n()
const { proxy } = getCurrentInstance() as ComponentInternalInstance

const { EnableStatusEnum } = toRefs<any>(proxy?.useEnum('enable_status_enum'))

const wechatRef = ref()
const mbankRef = ref()

const state = reactive({
  page: {
    pageSize: 9999,
    pageNum: 1,
  },
  total: 0,
  loading: false,
  query: {},
})

// 字典
const column = ref<IColumn[]>([
  {
    label: t('system.payment.name'),
    searchLabelWidth: 120,
    key: 'name',
    type: 'i18nString',
    search: false,
    formHide: false,
    colSpan: 22,
  },
  {
    label: t('system.payment.properties'),
    key: 'properties',
    hide: true,
    search: false,
    formHide: false,
    colSpan: 22,
  },
  {
    label: t('system.payment.status'),
    key: 'status',
    type: 'select',
    tableWidth: 120,
    componentProps: {
      options: EnableStatusEnum,
    },
    search: false,
    formHide: true,
    slot: true,
  },
  {
    label: t('crud.table.column.createTime'),
    key: 'createTime',
    tableWidth: 180,
    sortable: true,
    search: false,
    type: 'date',
    formHide: true,
  },
  {
    label: t('crud.table.column.note'),
    key: 'note',
    hide: true,
    formHide: false,
    search: false,
    colSpan: 22,
    componentProps: {
      type: 'textarea',
      rows: 2,
    }
  },
])
const crudRef = ref()
const data = ref<any[]>([])
const getData = () => {
  state.loading = true
  getPaymentList({
    ...state.page,
    ...state.query,
  })
    .then((res) => {
      data.value = res.data.rows
      state.total = res.data.total
    })
    .finally(() => {
      state.loading = false
    })
}

const handleSuccess = ()=>{
  getData()
}

/** 状态修改  */
const handleStatusChange = async (row: any) => {
  if(!row.id)  return
  try {
    await proxy?.$modal.confirm(row.status === 'Enabled' ? t('crud.action.tooltip.enable') : t('crud.action.tooltip.disabled'))
    await changeStatus(row.id, row.status)
    ElMessage.success(t('operate.tooltip.success'))
    getData()
  } catch (err) {
    row.status = row.status === 'Enabled' ? 'Disabled' : 'Enabled'
  }
}

//配置属性
const handleProperties = async (row: PaymentVO) => {
  if(row.identifier === 'Wechat') {
    wechatRef.value.openDialog(row)
  } else if(row.identifier === 'Mbank') {
    mbankRef.value.openDialog(row)
  } else {
    ElMessage.warning(t('operate.tooltip.not_configuration'))
  }
}

const options = reactive({
  ref: 'crudRef',
  data,
  column,
})
</script>

<style lang="scss" scoped></style>
